# encoding:utf-8
import json
import re
import requests
from bs4 import BeautifulSoup


from Hero import Hero
from Talent import Talent
from Skill import Skill


# region heroes

heroes = []
heroSoup = BeautifulSoup(
    requests.get("https://wiki.biligame.com/arknights/index.php?title=分类:干员").text,
    "html.parser",
)
heroNum = re.search(
    r"共(\d+)个页面", heroSoup.select("#mw-pages > p")[0].text, re.MULTILINE
).group(1)
pageLink = (
    f"""https://wiki.biligame.com{heroSoup.select("#mw-pages > a")[1].attrs["href"]}"""
)
for i in range(int((int(heroNum) + 200 - 1) / 200)):
    heroSoup = (
        BeautifulSoup(requests.get(pageLink).text, "html.parser")
        if i != 0
        else heroSoup
    )
    pageLink = f"""https://wiki.biligame.com{heroSoup.select("#mw-pages > a")[1].attrs["href"]}"""
    all_heroes = heroSoup.select("#mw-pages > div > div")[0].select("a")
    for j in all_heroes:
        heroName = j.text
        if heroName == "模板:干员@" or heroName == "":
            continue
        heroInfoSoup = BeautifulSoup(
            requests.get(
                f"https://wiki.biligame.com/arknights/index.php?title={heroName}&action=edit"
            ).text,
            "html.parser",
        )
        heroInfo = heroInfoSoup.select("#wpTextbox1")[0].text
        heroes.append(Hero(heroInfo))
        print(f"干员: {heroName}")

with open("heroes.json", "w", encoding="utf-8") as f:
    f.write(
        json.dumps(
            heroes,
            ensure_ascii=False,
            indent=2,
            sort_keys=False,
            separators=(",", ": "),
        )
    )

# endregion


# # region talents

# talents = []
# talentSoup = BeautifulSoup(
#     requests.get(
#         "https://wiki.biligame.com/tdj/index.php?title=分类:天赋&action=edit&redlink=1"
#     ).text,
#     "html.parser",
# )
# all_talents = talentSoup.select("#mw-pages > div > div")[0].select("a")
# for i in all_talents[2:]:
#     talentName = i.text
#     talentInfoSoup = BeautifulSoup(
#         requests.get(
#             f"https://wiki.biligame.com/tdj/index.php?title={talentName}&action=edit"
#         ).text,
#         "html.parser",
#     )
#     talentInfo = talentInfoSoup.select("#wpTextbox1")[0].text
#     talents.append(Talent(talentInfo))
#     print(f"天赋: {talentName}")

# with open("talents.json", "w", encoding="utf-8") as f:
#     f.write(
#         json.dumps(
#             talents,
#             ensure_ascii=False,
#             indent=2,
#             sort_keys=False,
#             separators=(",", ": "),
#         )
#     )

# # endregion

# # region skills

# skills = []
# skillSoup = BeautifulSoup(
#     requests.get("https://wiki.biligame.com/tdj/绝学列表").text, "html.parser"
# )
# all_skills = skillSoup.select("#CardSelectTr > tbody > tr")[1:]
# for i in all_skills:
#     skillName = i.select("td")[1].select("a")[0].text
#     if skillName != "天机尺":
#         skillInfoSoup = BeautifulSoup(
#             requests.get(
#                 f"https://wiki.biligame.com/tdj/index.php?title=绝学/{skillName}&action=edit"
#             ).text,
#             "html.parser",
#         )
#         skillInfo = skillInfoSoup.select("#wpTextbox1")[0].text
#         skills.append(Skill(skillInfo))
#     print(f"绝学：{skillName}")

# with open("skills.json", "w", encoding="utf-8") as f:
#     f.write(
#         json.dumps(
#             skills,
#             ensure_ascii=False,
#             indent=2,
#             sort_keys=False,
#             separators=(",", ": "),
#         )
#     )

# # endregion

print("OK")
